﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ComLib.ConfigService;
using ComLib.ComFun;

namespace ProcessConfig.ConfigType
{
    /// <summary>
    /// 对齐算法因子
    /// </summary>
    [XmlRootAttribute("AlignmentAlgorithmFactor", Namespace = "ProcessConfig.ConfigType", IsNullable = false)]
    public class AlignmentAlgorithmFactor : XmlSerializeBase
    {
        public AlignmentAlgorithmFactor()
        {

        }

        /// <summary>
        /// 镜头初始位置(像素)
        /// </summary>
        private int _nInitialPosition = 300;
        public int nInitialPosition
        {
            get { return _nInitialPosition; }
            set { _nInitialPosition = value; }
        }

        /// <summary>
        /// 显示器物理高度(cm)
        /// </summary>
        private double _screenSize = 70;
        public double ScreenSize
        {
            get { return _screenSize; }
            set { _screenSize = value; }
        }

        /// <summary>
        /// 显示器垂直分辨率
        /// </summary>
        private int _nScreenVerticalResolution = 1920;
        public int nScreenVerticalResolution
        {
            get { return _nScreenVerticalResolution; }
            set { _nScreenVerticalResolution = value; }
        }

        /// <summary>
        /// 电机移动1000步的距离(cm)
        /// </summary>
        private double _thousandStepsLength = 4.5;
        public double ThousandStepsLength
        {
            get { return _thousandStepsLength; }
            set { _thousandStepsLength = value; }
        }

        /// <summary>
        /// 电机移动1步经过的像素点数
        /// </summary>
        public double PixelSizePerStep
        {
            get
            {
                double val = (_thousandStepsLength * (double)nScreenVerticalResolution) / ((double)ScreenSize * (double)1000);
                return val;
            }
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            AlignmentAlgorithmFactor obj = (AlignmentAlgorithmFactor)Serializer.LoadFromXml(ConfigFilePath, this.GetType());
            if (obj != null)
            {
                this._nInitialPosition = obj._nInitialPosition;
                this._nScreenVerticalResolution = obj._nScreenVerticalResolution;
                this._nVer = obj._nVer;
                this._screenSize = obj._screenSize;
                this._thousandStepsLength = obj._thousandStepsLength;
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("对齐算法因子集: ");
            FillFieldValueStringDelegate FillString =
              (string field, object val) =>
              {
                  builder.Append("[");
                  builder.Append(field);
                  builder.Append("]: ");
                  builder.Append(val.ToString() + "; ");
              };
            FillString("[镜头初始位置]:", _nInitialPosition);
            FillString("[显示器物理高度]:", _screenSize);
            FillString("[显示器垂直分辨率]:", _nScreenVerticalResolution);
            FillString("[电机移动1000步的距离(cm)]:", _thousandStepsLength);
            FillString("[电机移动1步经过的像素点数]:", PixelSizePerStep);
            return builder.ToString();
        }
    }
}
